package service.impl;

import Exception.OrderException;
import service.OrderService;
import ssm.dao.impl.OrderDaoImpl;
import ssm.pojo.*;

import util.Dbutil;
import util.UUIDUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Collection;
import java.util.Date;
import java.util.List;

public class OrderServiceImpl implements OrderService {

    /**
     * 添加订单 封装获取订单
     *
     * @param user 用户
     * @param cart 购物车
     * @return 订单项
     */
    public Orders addOrder(User user, Cart cart) {
        Connection connection = null;
        try {
            OrderDaoImpl orderDao = new OrderDaoImpl();
            Orders orders = new Orders();
            //生成随机订单编号
            String oid = UUIDUtils.getId();
            //获取下单时间
            Date date = new Date();
            //获取时间戳
            LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
            orders.setOid(oid);
            orders.setOrdertime(localDateTime);
            orders.setTotal(cart.getCountPrice());
            orders.setUser(user);

            //将购物车里的商品项添加到订单项中
            Orderitem orderitem = null;
            Collection<CartItem> cartItem = cart.getCartItem();
            //添加事务
            connection = Dbutil.getConnection();
            connection.setAutoCommit(false);
            for (CartItem c : cartItem) {
                orderitem = new Orderitem();
                orderitem.setItemid(UUIDUtils.getId());
                orderitem.setCount(c.getCount());
                orderitem.setSubtotal(c.getPrice());
                orderitem.setProduct(c.getProduct());
                orderitem.setOrders(orders);
                //向订单中添加订单项
                orders.getItems().add(orderitem);

            }
            int i = orderDao.addOrder(orders);
            //int p = 10/0;
            List<Orderitem> items = orders.getItems();
            for (Orderitem it : items) {
                orderDao.addOrderitem(it);
            }
            connection.commit();
            return orders;
        } catch (Exception e) {
            try {
                connection.rollback();
                throw new OrderException("null");
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 分页查询用户订单
     *
     * @param user   用户
     * @param number 当前页数
     * @param total  每页显示条数 默认4条
     * @return 分页实例
     */
    public PageBean<Orders> orderByPage(User user, int number, int total) {
        try {
            OrderDaoImpl orderDao = new OrderDaoImpl();
            ProductServiceImpl productService = new ProductServiceImpl();

            //查询该页的订单列表
            List<Orders> orderBU = orderDao.findOrderBU(user, number, total);
            if (orderBU.size()!=0) {
                for (Orders o : orderBU) {
                    //查询每个订单的订单项集合
                    List<Orderitem> orderitems = orderDao.fintOrderItemBO(o);
                    //放入到每个订单中
                    o.setItems(orderitems);
                    //遍历订单项集合 获取订单项
                    for (Orderitem orderitem : orderitems) {
                        //查询商品id 将商品放到订单项中
                        String pid = orderitem.getPid();
                        Product product = productService.findPById(pid);
                        orderitem.setProduct(product);
                    }
                }
                //查询总订单数
                int countOrder = orderDao.findCountOrder(user);
                PageBean<Orders> pageBean = new PageBean<Orders>(number, total, countOrder, orderBU);
                System.out.println(pageBean);
                return pageBean;
            } else {
                return null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

}
